package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();//添加后的菜品id

        //添加口味表
        //flavor是口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        // 在添加口味表数据之前 将菜品id设置进去
        if (flavors != null && flavors.size() > 0) {//健壮性判断  容器里面有没有东西  有东西才遍历
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
        }
        dishFlavorMapper.insert(flavors);//批量插入

    }

    /**
     * 分页查询菜品
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("开始分页查询");
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        Long total = page.getTotal();
        List<DishVO> records = page.getResult();
        log.info("分页查询结果:{}", records);
        return new PageResult(total, records);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            Integer status = dish.getStatus();
            if (status == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        for (Long id : ids) {
            List<Long> setmealId = setMealDishMapper.getSetmealIdsByDishIds(id);
            if(setmealId!=null && setmealId.size()>0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        for (Long id : ids) {
            dishMapper.delete(id);
            Long dishId = id;
            dishFlavorMapper.deleteByDishId(dishId);
        }
    }

    /**
     * 根据id查询菜品和口味
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        log.info("口味信息:{}", flavors);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);


        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteByDishId(dishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        dishFlavorMapper.insert(flavors);
    }

    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {

//        dish.setStatus(StatusConstant.ENABLE);
//      dish.setCategoryId(categoryId);
        List<Dish> dishVOList =  dishMapper.getDishByCategoryId(categoryId);
        log.info("套餐包含菜品:{}",dishVOList);
        return dishVOList;

    }

    @Override
    public void updateStatus(Integer status, Long id) {
        //1:修改菜品信息 --修改菜品状态
        Dish dish = new Dish();//dish封装需要修改信息
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);

        //2:菜品停售时  包含菜品的套餐也要停售
        if (status == StatusConstant.DISABLE) {//要停售菜品
            // 先根据菜品的id 查询出 关联的套餐id
            //  因为 这个功能的参数需要一个集合  将我的菜品id 放到集合中
//            List<Long> ids = new ArrayList<>();
//            ids.add(id);//将我们的菜品id添加到集合
//            log.info("id:{}",ids);
            //这个功能已经实现了 setmealdishMapper
            List<Long> setmealIds = setMealDishMapper.getSetmealIdsByDishIds(id);
            log.info("setmealIds:{}",setmealIds);
            // 到底有没有关联过套餐呢 ？
            if (setmealIds != null && setmealIds.size() > 0) {//菜品关联上了套餐
                // 停售该套餐  套餐要一个个停售
                for (Long setmealId : setmealIds) {
                    //修改 setmeal表  找到指定的 套餐完成 状态的修改
                    // update setmeal set status=0 ,update_time=now(),update_user=1 where id=1;
                    //先来封装被修改的setmeal信息
                    Setmeal setmeal = new Setmeal();
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setmeal.setId(setmealId);
                    //交给 setmealMapper去做
                    setMealMapper.update(setmeal);
                }
            }else log.info("无值");


        }
    }
}